// JavaScript Document

function FarmApp(canvas)
{
    if (this instanceof FarmApp)
    {
        var ctx = canvas.getContext("2d");
        this.ctx = ctx;
        this.canvas = canvas;
    }
    else
    {
        return new FarmApp(canvas);
    }
}

FarmApp.prototype.init = function()
{
	log.info("start app.");
	this.cursorVisible = false;
	this.states = new Array(STATE_NUM);
	for (var i = 0; i < STATE_NUM; i++)
	{
		this.states[i] = null;
	}
	// state manager here
	this.lastTime = new Date();
	this.curState = null;
	this.nextState = null;
	this.cursor = null;
	this.cursorPos = new Point(0, 0);
	this.cursorOffset = new Point(0, 0);
	this.setImgCursor(g_imgMgr.getImage(CURSOR_COMMON));
	this.setState(STATE_FARM);
}

FarmApp.prototype.getCursorPos = function()
{
	return this.cursorPos;
}

FarmApp.prototype.setImgCursor = function(img, offset)
{
	var _offset = new Point(0, 0);
	if (offset !== undefined)
	{
		_offset.setValue(offset);
	}
	this.cursor = img;
	this.cursorOffset.setValue(_offset);
}

FarmApp.prototype.startCursorAnimation = function()
{
	if (this.cursor && this.cursor.start)
	{
		this.cursor.start();
	}
}

FarmApp.prototype.setState = function(state)
{
	if (this.states[state] == null)
	{
		switch(state)
		{
		case STATE_FARM:
			this.states[state] = new FarmState(this.ctx);
			break;
		default:
			assert(false, "should not go here. state id = " + state);
			break;
		}
	}
	this.nextState = this.states[state];
}

FarmApp.prototype.onTimer = function()
{
	// state manage first.
	if (this.nextState != this.curState)
	{
		if (this.curState)
		{
			this.curState.onExit();
		}
		this.nextState.onEnter();
		this.curState = this.nextState;
	}
	// update cur state.
	if (this.curState)
	{
		var now = new Date();
		var interval = now.getTime() - this.lastTime.getTime();
		this.lastTime = now;
		this.curState.update(interval * 0.001);
		this.curState.draw();
		if (this.cursor)
		{
			if (this.cursor.update)
			{
				this.cursor.update(interval * 0.001);
			}
			if (this.cursorVisible)
			{
				this.cursor.draw(this.ctx, this.cursorPos.x + this.cursorOffset.x, this.cursorPos.y + this.cursorOffset.y);
			}
		}
	}
}

FarmApp.prototype.onMouseDown = function(e)
{
	var pt = new Point(e.pageX - g_canvasOffsetX, e.pageY - g_canvasOffsetY);
	this.cursorPos.setValue(pt);
	if (this.curState)
	{
		this.curState.onMouseDown(pt);
	}
}

FarmApp.prototype.onMouseMove = function(e)
{
	var pt = new Point(e.pageX - g_canvasOffsetX, e.pageY - g_canvasOffsetY);
	this.cursorPos.setValue(pt);
	if (this.curState)
	{
		this.curState.onMouseMove(pt);
	}
	this.cursorVisible = true;
}

FarmApp.prototype.onMouseUp = function(e)
{
	var pt = new Point(e.pageX - g_canvasOffsetX, e.pageY - g_canvasOffsetY);
	this.cursorPos.setValue(pt);
	if (this.curState)
	{
		this.curState.onMouseUp(pt);
	}
}

FarmApp.prototype.onMouseOut = function()
{
	if (this.curState && this.curState.onMouseOut)
	{
		this.curState.onMouseOut();
	}
	this.cursorVisible = false;
}
